﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ornament.Messages.Contents;
using Ornament.Messages.Contents.Stores;
using Ornament.Messages.Dao;
using Ornament.Messages.Dao.NHibernateImple;
using Ornament.NHibernates;

namespace Ornament.Messages.Core.Test
{
    /// <summary>
    /// Summary description for Persistent
    /// </summary>
    [TestClass]
    public class Persistent
    {
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        public static InfoType ExistType { get; set; }

        #region Additional test attributes

        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize]
        public static void MyClassInitialize(TestContext testContext)
        {
            ExistType = new InfoType("Name");
            var dao = new InfoTypeDao();
            dao.SaveOrUpdate(ExistType);
        }

        //
        // Use ClassCleanup to run code after all tests in a class have run
        [ClassCleanup]
        public static void MyClassCleanup()
        {
        }

        //
        // Use TestInitialize to run code before running each test 
        [TestInitialize]
        public void MyTestInitialize()
        {
        }

        //
        // Use TestCleanup to run code after each test has run
        [TestCleanup]
        public void MyTestCleanup()
        {
            

            //if (bf.GetSession().Transaction.IsActive)
            //    bf.GetSession().Transaction.Commit();
            //bf.CloseWorkSpace();
        }

        //

        #endregion

        [TestMethod]
        public void Test_InfoType_CUDPersistent()
        {
            //test type;

            var type = new InfoType("Name");


            var dao = new InfoTypeDao();
            dao.SaveOrUpdate(type);
            //dao.Flush();

            //test update.
            InfoType storeType = dao.Get(type.Id);
            Assert.AreEqual(type.Name, storeType.Name);
            storeType.Name = "name1";
            dao.SaveOrUpdate(storeType);
            //dao.Flush();

            dao.Delete(type);
            //dao.Flush();
        }

        [TestMethod]
        public void Save_Info_With_A_Boo()
        {
            var session = SessionManager.Instance.CurrentSession;

            var info = new Info("publisher", ExistType);
            info.Content = new GenericContent<Boo>();
            info.Content.Store = new FileStore("");

            var boo = new Boo();

            session.Save(boo);
            session.Flush();

            info.Content.Value = boo;
            var infoDao = new InfoDao();
            infoDao.SaveOrUpdate(info);
            

            //be check;

            info = infoDao.Get(info.Id);
            var actual = (Boo)info.Content.Value;
            Assert.AreEqual(boo.Id, ((Boo)info.Content.Value).Id);
        }

        [TestMethod]
        public void SAVE_Info_With_String()
        {
            var info = new Info("publisher", ExistType);
            info.Content = new GenericContent<string>();
            info.Content.Store = new FileStore("");
            info.Content.Value = "value of content";

            var infoDao = new InfoDao();
            infoDao.SaveOrUpdate(info);
            
        }
    }


    public class Boo
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
    }
}